
/**
 * @Project : Clion_ProgrammingExercises
 * @File : A1006
 * @Author : WangRuoyu 
 * @Date : 2023/5/8 20:08
 */

#include "iostream"

using namespace std;

bool compare(string a, string b) {
    for (int i = 0; i < a.size(); ++i) {
        if (a[i] > b[i]) {
            return false;
        } else if (a[i] < b[i]) {
            return true;
        }
    }
    return false;
}

int main() {
    int x;
    cin >> x;
    string early;
    string late;
    string min;
    string max;
    while (x--) {
        string a, b, c;
        cin >> a >> b >> c;
        if (early.empty()) {
            early = a;
            min = b;
            late = a;
            max = c;
            continue;
        }
        if (compare(b, min)) {
            early = a;
            min = b;
        }
        if (compare(max, c)) {
            late = a;
            max = c;
        }
    }

    cout << early << " " << late;

    return 0;
}